package com.bingxue.edu.teacher.service;

import com.bingxue.edu.management.education.service.LessonEvaluationService;
import com.bingxue.edu.management.education.service.LessonService;
import com.bingxue.edu.teacher.model.query.BaseTeacherQuery;
import com.bingxue.edu.teacher.model.resp.LessonEvaluationDetailResp;
import com.bingxue.edu.teacher.model.resp.LessonEvaluationResp;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.bingxue.edu.management.education.model.entity.table.ClassInfoTableDef.CLASS_INFO;
import static com.bingxue.edu.management.education.model.entity.table.LessonEvaluationTableDef.LESSON_EVALUATION;
import static com.bingxue.edu.management.education.model.entity.table.LessonTableDef.LESSON;
import static com.bingxue.edu.management.education.model.entity.table.TimeTemplateTableDef.TIME_TEMPLATE;

@Slf4j
@Service
@RequiredArgsConstructor
public class ParentEvaluationService {

    private final LessonService lessonService;
    private final LessonEvaluationService lessonEvaluationService;
    public List<LessonEvaluationResp> getLessonEvaluation(BaseTeacherQuery query) {
        QueryWrapper queryWrapper = buildLessonEvaluationWrapper();
        queryWrapper.where(LESSON.LEAD_TEACHER_ID.eq(query.getLeadTeacherId())).and(LESSON.ASSIST_TEACHER_ID.eq(query.getAssistTeacherId()))
                .groupBy(CLASS_INFO.ID,CLASS_INFO.NAME,LESSON.ID,LESSON.LESSON_DATE,LESSON.TITLE,TIME_TEMPLATE.START_TIME
                        ,TIME_TEMPLATE.END_TIME,TIME_TEMPLATE.NAME,LESSON_EVALUATION.LESSON_ID,LESSON_EVALUATION.SCORE);
        return lessonService.getMapper().selectListByQueryAs(queryWrapper, LessonEvaluationResp.class);
    }

    private QueryWrapper buildLessonEvaluationWrapper() {
        return QueryWrapper.create()
                .select(
                        CLASS_INFO.ID.as(LessonEvaluationResp::getClassId),
                        CLASS_INFO.NAME.as(LessonEvaluationResp::getClassName),
                        LESSON.ID.as(LessonEvaluationResp::getLessonId),
                        LESSON.LESSON_DATE,
                        LESSON.TITLE,
                        TIME_TEMPLATE.START_TIME,
                        TIME_TEMPLATE.END_TIME,
                        TIME_TEMPLATE.NAME.as(LessonEvaluationResp::getTimeTempleName),
                        QueryMethods.count(LESSON_EVALUATION.LESSON_ID).as(LessonEvaluationResp::getParentEvaluationCount),
                        QueryMethods.avg(LESSON_EVALUATION.SCORE).as(LessonEvaluationResp::getAvgScore)
                ).from(LESSON)
                .leftJoin(CLASS_INFO).on(LESSON.CLASS_ID.eq(CLASS_INFO.ID))
                .leftJoin(TIME_TEMPLATE).on(CLASS_INFO.TIME_TEMPLATE_ID.eq(TIME_TEMPLATE.ID))
                .leftJoin(LESSON_EVALUATION).on(LESSON.ID.eq(LESSON_EVALUATION.LESSON_ID));
    }

    public List<LessonEvaluationDetailResp> getLessonEvaluationDetail(String id) {
        return lessonEvaluationService.getLessonEvaluationDetail(id);
    }
}
